// index.js
const util = require('../../../util/util.js')
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    typeF: false,
    typeC: false,
    dataTable: [], // 原始数据
    roadData: [], // 转化后的数据
    timeStart: null, //时分秒，根据需要选择
    roadName: '',
    CLIENT_NO: '',
    clientNoIndex: 0,
    clientNoList: [],
    search: [],
    searchValue: '',
    popup: {
      show: false,
      list: [],
    }
    // showPopupCarList
  },
  onLoad: function () {
    var that = this;
    const currentDate = new Date();
    currentDate.setDate(currentDate.getDate() - 1);
    that.setData({
      timeStart: util.formatDate(currentDate),
      CLIENT_NO: wx.getStorageSync("CLIENT_NO")
    })
    this.getClientNoListInfo()
  },
  onInput (e) {
    wx.hideKeyboard();
    var that = this;
    
    wx.request({
      url: 'https://www.ejingyou.cn/ServerHuanwei/CompanyapiBDEapi/Vehicle/GetRoadInfoApp',
      data: {
        ClientNo: this.data.CLIENT_NO,
        ROADNAME: e.detail.value
      },
      header: {
        'content-type': 'application/json' // 默认值     
      },
      success: (res) => {
        setTimeout(() => {
          this.setData({
            'popup.show': true,
            'popup.list': res.data
          });
        }, 30); // 延迟30ms确保键盘关闭完成
      }
    })
  },
  // 点击道路查询结果某一行
  onPopupCell (e) {
    console.log(e.detail)
    this.setData({
      roadName: this.data.popup.list[e.detail].Rname,
      searchValue: this.data.popup.list[e.detail].Rname,
    });
    this.getListInfo()
    console.log(this.data.roadName)
  },
  // 关闭道路查询结果列表
  onPopupClose () {
    this.setData({
      'popup.show': false,
    });
  },
  openTypeF () {
    this.setData({
      typeF: true
    })
  },
  openTypeC () {
    // console.log(this.data.clientNoList)
    this.setData({
      typeC: true
    })
  },
  selectDateStartChange (e) {
    this.setData({
      timeStart: e.detail.value
    })
  },
  selectClientNoChange (e) {
    const index = e.detail.value;
    const CLIENT_NO = this.data.clientNoList[index].value;
    this.setData({
      clientNoIndex: index,
      CLIENT_NO,
      typeC: false
    });
  },
  // getDateTimeChange () {
  //   this.getListInfo(this.data.activeTab)
  // },
  onViewTap (event) {
    const car = event.currentTarget.dataset.id;
    this.data.roadData.forEach(element => {
      if (element.VID === car) {
        wx.navigateTo({
          url: `../../../page/mapHistoryTrajectory/mapHistoryTrajectory?VID=${car}&SD=${element.SD}&ED=${element.ED}`
        })
      }
    });
  },
  // 获取组织机构信息
  getClientNoListInfo () {
    var that = this;
    wx.request({
      url: 'https://www.ejingyou.cn/ServerHuanwei/CompanyapiBDEapi/Vehicle/GetClientInfo',
      data: {
        client_no: this.data.CLIENT_NO,
      },
      header: {
        'content-type': 'application/json' // 默认值     
      },
      success: (res) => {
        const originalData = res.data;
        const pickerData = originalData.map(item => ({
          value: item.CLIENT_GROUP_NO,
          label: item.CLIENT_GROUP_NAME
        }));
        that.setData({
          clientNoList: pickerData,
          CLIENT_NO: pickerData[0].value // 默认选中第一个
        });
        this.getListInfo()
      }
    })
  },
  // 获取信息
  getListInfo () {
    this.setData({
      typeC: false,
      typeF: false
    });
    wx.request({
      url: 'https://www.ejingyou.cn/ServerHuanwei/CompanyapiBDEapi/Vehicle/GetRoadWorkVehicleInfo3App',
      data: {
        ClientNo: this.data.CLIENT_NO,
        Date: this.data.timeStart,
        ROADNAME: this.data.roadName
      },
      header: {
        'content-type': 'application/json' // 默认值     
      },
      success: (res) => {
        // console.log(res.data)
        const englishData = res.data.map(item => ({
          roadGrade: item.车牌号,
          VID: item.VID,
          SD: item.SD,
          ED: item.ED,
          qualified: item.车辆类型,
          unqualified: item.作业次数,
          qualifiedRate: item.达标率
        }));
        this.setData({
          roadData: englishData,
          'popup.show': false,
        });
      }
    })
  },
})